package br.ufal.ic.fracao.agent.resolution.operation.multiplication;

import java.io.IOException;

import org.jdom.JDOMException;

import br.ufal.ic.fracao.especialist.domain.Item;
import br.ufal.ic.fracao.especialist.inference.InferenceAlgorithm;

public class MultiplicationAdapter {
	
	public String getSolution(int n1, int d1, int n2, int d2) throws IOException, JDOMException{
		InferenceAlgorithm algorithm = new InferenceAlgorithm();
				        
        Item fato1 = new Item();
        fato1.setDescription("Multiplication");
        fato1.setValue(String.valueOf(true));
        fato1.setCertaintyFactor(0.2);
        algorithm.addToMemoryWorking(fato1);
        
        Item conclusion1 = new Item("(n1*n2)/(d1*d2)", "true");
        String result1 = algorithm.doForwardChaining(conclusion1, false);
                
        if (result1.equalsIgnoreCase("(n1*n2)/(d1*d2)")) {
        	int numerator = (n1 * n2);
        	int denominator = (d1 * d2);
        	if(denominator == 1){
        		return String.valueOf(numerator);
        	}else{
        		return numerator + "/" + denominator;
        	}
        }
		return null;
	}
	
	public int mmc(int m, int n){
		int x = m;
		int y = n;
		int div = 2;
		int mmc = 1;
		while((x != 1) || (y != 1)){
			while((x % div == 0) || (y % div == 0)){
				mmc = mmc * div;
				if(x % div == 0){
					x = (int)x/div;
				}
				if(y % div == 0){
					y = (int)y/div;
				}
			}
			div++;
		}
		return mmc;
	}
}
